package org.jeecg.modules.travel.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.travel.dto.theme.ThemeAddDto;
import org.jeecg.modules.travel.dto.theme.ThemeEditDto;
import org.jeecg.modules.travel.dto.theme.ThemePageDto;
import org.jeecg.modules.travel.entity.JhTheme;
import org.jeecg.modules.travel.service.IJhThemeService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.travel.vo.theme.JhThemeTreeVo;
import org.jeecg.modules.travel.vo.theme.ThemeListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 主题分类
 * @Author: jeecg-boot
 * @Date:   2025-04-19
 * @Version: V1.0
 */
@Tag(name="主题分类")
@RestController
@RequestMapping("/travel/jhTheme")
@Slf4j
public class JhThemeController extends JeecgController<JhTheme, IJhThemeService> {

	@Autowired
	private IJhThemeService jhThemeService;
	
	/**
	 * 分页列表查询
	 *
	 * @param dto
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "主题分类-分页列表查询")
	@Operation(summary="主题分类-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<JhTheme>> queryPageList(ThemePageDto dto, HttpServletRequest req) {
		JhTheme theme = new JhTheme();
		BeanUtils.copyProperties(dto,theme);
        QueryWrapper<JhTheme> queryWrapper = QueryGenerator.initQueryWrapper(theme, req.getParameterMap());
		Page<JhTheme> page = new Page<JhTheme>(dto.getPageNo(), dto.getPageSize());
		IPage<JhTheme> pageList = jhThemeService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 树形结构查询
	  */
	 /**
	  * 获取完整的主题分类树形结构
	  */
	 @Operation(summary="风景分类-树")
	 @GetMapping("/tree")
	 public Result<List<JhThemeTreeVo>> getThemeTree() {
		 List<JhThemeTreeVo> tree = jhThemeService.getThemeTree();
		 return Result.ok(tree);
	 }
	/**
	 *   添加
	 *
	 * @param dto
	 * @return
	 */
	@AutoLog(value = "主题分类-添加")
	@Operation(summary="主题分类-添加")
	@RequiresPermissions("org:jh_theme:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody @Valid ThemeAddDto dto) {
		String res = jhThemeService.saveTheme(dto);
		if (StringUtils.isNotBlank(res)) {
			return Result.error(res);
		}
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param dto
	 * @return
	 */
	@AutoLog(value = "主题分类-编辑")
	@Operation(summary="主题分类-编辑")
	@RequiresPermissions("org:jh_theme:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody @Valid ThemeEditDto dto) {
		String res = jhThemeService.updateThemeById(dto);
		if (StringUtils.isNotBlank(res)) {
			return Result.error(res);
		}
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "主题分类-通过id删除")
	@Operation(summary="主题分类-通过id删除")
	@RequiresPermissions("org:jh_theme:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		String res = jhThemeService.removeThemeById(id);
		if (StringUtils.isNotBlank(res)) {
			return Result.error(res);
		}
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "主题分类-批量删除")
	@Operation(summary="主题分类-批量删除")
	@RequiresPermissions("org:jh_theme:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.jhThemeService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "主题分类-通过id查询")
	@Operation(summary="主题分类-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<JhTheme> queryById(@RequestParam(name="id",required=true) String id) {
		JhTheme jhTheme = jhThemeService.getById(id);
		if(jhTheme==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(jhTheme);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param jhTheme
    */
    @RequiresPermissions("org:jh_theme:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, JhTheme jhTheme) {
        return super.exportXls(request, jhTheme, JhTheme.class, "主题分类");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("org:jh_theme:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, JhTheme.class);
    }


	/** =================================== 移动端 ================================================= */

	@Operation(summary="主题分类-移动端列表")
	@GetMapping(value = "/app/appThemeList")
	public Result<List<ThemeListVo>> appThemeList() {
		return Result.OK(jhThemeService.appThemeList());
	}

}
